home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue69 / OpenGL / DGLUT.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-12-29  |  28.8 KB  |  621 lines

  1. { DGLUT.PAS }
  2.  
  3. { Bob Crawford
  4.   F.L.A.S.K.
  5.   June, 1997 }
  6.  
  7. { DGLUT is an Object Pascal translation of a small part of Mark Kilgard's
  8.   GLUT library for OpenGL. Included here are just the shape-drawing routines
  9.   from glut_shapes.c and the teapot routines from glut_teapot.c. Following
  10.   is the original copyright notices from those files. }
  11.  
  12. { Copyright (c) Mark J. Kilgard, 1994.
  13.  
  14. /**
  15. (c) Copyright 1993, Silicon Graphics, Inc.
  16.  
  17. ALL RIGHTS RESERVED
  18.  
  19. Permission to use, copy, modify, and distribute this software
  20. for any purpose and without fee is hereby granted, provided
  21. that the above copyright notice appear in all copies and that
  22. both the copyright notice and this permission notice appear in
  23. supporting documentation, and that the name of Silicon
  24. Graphics, Inc. not be used in advertising or publicity
  25. pertaining to distribution of the software without specific,
  26. written prior permission.
  27.  
  28. THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
  29. "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
  30. OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  31. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  32. EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
  33. ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
  34. CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  35. INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
  36. SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
  37. NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
  38. OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  39. ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
  40. PERFORMANCE OF THIS SOFTWARE.
  41.  
  42. US Government Users Restricted Rights
  43.  
  44. Use, duplication, or disclosure by the Government is subject to
  45. restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  46. (c)(1)(ii) of the Rights in Technical Data and Computer
  47. Software clause at DFARS 252.227-7013 and/or in similar or
  48. successor clauses in the FAR or the DOD or NASA FAR
  49. Supplement.  Unpublished-- rights reserved under the copyright
  50. laws of the United States.  Contractor/manufacturer is Silicon
  51. Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
  52. 94039-7311.
  53.  
  54. OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  55. }
  56.  
  57. unit DGLUT;
  58.  
  59. interface
  60.  
  61. uses
  62.   OpenGL12;
  63.  
  64. type
  65.   TGLfloat3v =
  66.     array[0..2] of TGLfloat;
  67.   TInteger3v =
  68.     array[0..2] of Integer;
  69.  
  70. const
  71.   BoxPoints :
  72.     Array[0..5, 0..2] of TGLfloat =
  73.       ( (-1,  0,  0),
  74.         ( 0,  1,  0),
  75.         ( 1,  0,  0),
  76.         ( 0, -1,  0),
  77.         ( 0,  0,  1),
  78.         ( 0,  0, -1) );
  79.  
  80.   BoxFaces :
  81.     Array[0..5, 0..3] of TGLint =
  82.       ( (0, 1, 2, 3),
  83.         (3, 2, 6, 7),
  84.         (7, 6, 5, 4),
  85.         (4, 5, 1, 0),
  86.         (5, 6, 2, 1),
  87.         (7, 4, 0, 3) );
  88.  
  89.   { Octahedron data:
  90.       The octahedron produced is centered at the origin and has radius 1.0 }
  91.   OctData :
  92.     Array[0..5] of TGLfloat3v =
  93.       ( (1.0, 0.0, 0.0),
  94.         (-1.0, 0.0, 0.0),
  95.         (0.0, 1.0, 0.0),
  96.         (0.0, -1.0, 0.0),
  97.         (0.0, 0.0, 1.0),
  98.         (0.0, 0.0, -1.0) );
  99.  
  100.   OctIndex :
  101.     Array[0..7] of TInteger3v =
  102.       (  (0, 4, 2),
  103.          (1, 2, 4),
  104.          (0, 3, 4),
  105.          (1, 4, 3),
  106.          (0, 2, 5),
  107.          (1, 5, 2),
  108.          (0, 5, 3),
  109.          (1, 3, 5) );
  110.  
  111.   { Icosahedron data:
  112.       These numbers are rigged to make an icosahedron of radius 1.0 }
  113.  
  114.   IcoX = 0.525731112119133606;
  115.   IcoZ = 0.850650808352039932;
  116.  
  117.   IcoData :
  118.     Array[0..11] of TGLfloat3v =
  119.       ( (-IcoX,     0,  IcoZ),
  120.         ( IcoX,     0,  IcoZ),
  121.         (-IcoX,     0, -IcoZ),
  122.         ( IcoX,     0, -IcoZ),
  123.         (    0,  IcoZ,  IcoX),
  124.         (    0,  IcoZ, -IcoX),
  125.         (    0, -IcoZ,  IcoX),
  126.         (    0, -IcoZ, -IcoX),
  127.         ( IcoZ,  IcoX,     0),
  128.         (-IcoZ,  IcoX,     0),
  129.         ( IcoZ, -IcoX,     0),
  130.         (-IcoZ, -IcoX,     0) );
  131.  
  132.   IcoIndex :
  133.     Array[0..19] of TInteger3v =
  134.       ( (0, 4, 1),
  135.         (0, 9, 4),
  136.         (9, 5, 4),
  137.         (4, 5, 8),
  138.         (4, 8, 1),
  139.         (8, 10, 1),
  140.         (8, 3, 10),
  141.         (5, 3, 8),
  142.         (5, 2, 3),
  143.         (2, 7, 3),
  144.         (7, 10, 3),
  145.         (7, 6, 10),
  146.         (7, 11, 6),
  147.         (11, 0, 6),
  148.         (0, 1, 6),
  149.         (6, 1, 10),
  150.         (9, 0, 11),
  151.         (9, 11, 2),
  152.         (9, 2, 5),
  153.         (7, 2, 11) );
  154.  
  155.   { Tetrahedron data }
  156.  
  157.   TetT = 1.73205080756887729;
  158.  
  159.   TetData :
  160.     Array[0..3] of TGLfloat3v =
  161.       ( ( TetT,  TetT,  TetT),
  162.         ( TetT, -TetT, -TetT),
  163.         (-TetT,  TetT, -TetT),
  164.         (-TetT, -TetT,  TetT) );
  165.  
  166.   TetIndex :
  167.     Array[0..3] of TInteger3v =
  168.       (  (0, 1, 3),
  169.          (2, 1, 0),
  170.          (3, 2, 0),
  171.          (1, 2, 3)  );
  172.  
  173. { Teapot stuff }
  174.  
  175.   { Rim, body, lid, and bottom data must be reflected in x
  176.    and y; handle and spout data across the y axis only. }
  177.  
  178.   PatchData :
  179.     Array[0..9, 0..15] of TGLint =
  180.     (
  181.       { Rim }
  182.       (102, 103, 104, 105,   4,   5,   6,   7,
  183.          8,   9,  10,  11,  12,  13,  14,  15),
  184.       { Body }
  185.       ( 12,  13,  14,  15,  16,  17,  18,  19,
  186.         20,  21,  22,  23,  24,  25,  26,  27),
  187.       ( 24,  25,  26,  27,  29,  30,  31,  32,
  188.         33,  34,  35,  36,  37,  38,  39,  40),
  189.       { Lid *}
  190.       ( 96,  96,  96,  96,  97,  98,  99, 100,
  191.        101, 101, 101, 101,   0,   1,   2,   3),
  192.       (  0,   1,   2,   3, 106, 107, 108, 109,
  193.        110, 111, 112, 113, 114, 115, 116, 117),
  194.       { Bottom }
  195.       (118, 118, 118, 118, 124, 122, 119, 121,
  196.        123, 126, 125, 120,  40,  39,  38,  37),
  197.       { Handle }
  198.       ( 41,  42,  43,  44,  45,  46,  47,  48,
  199.         49,  50,  51,  52,  53,  54,  55,  56),
  200.       ( 53,  54,  55,  56,  57,  58,  59,  60,
  201.         61,  62,  63,  64,  28,  65,  66,  67),
  202.       { Spout }
  203.       ( 68,  69,  70,  71,  72,  73,  74,  75,
  204.         76,  77,  78,  79,  80,  81,  82,  83),
  205.       ( 80,  81,  82,  83,  84,  85,  86,  87,
  206.         88,  89,  90,  91,  92,  93,  94,  95) );
  207.  
  208.   TeaData :
  209.     Array[0..126, 0..2] of TGLfloat =
  210.     ( (   0.2,       0,     2.7),
  211.       (   0.2,  -0.112,     2.7),
  212.       ( 0.112,    -0.2,     2.7),
  213.       (     0,    -0.2,     2.7),
  214.       (1.3375,       0, 2.53125),
  215.       (1.3375,  -0.749, 2.53125),
  216.       ( 0.749, -1.3375, 2.53125),
  217.       (     0, -1.3375, 2.53125),
  218.       (1.4375,       0, 2.53125),
  219.       (1.4375,  -0.805, 2.53125),
  220.       ( 0.805, -1.4375, 2.53125),
  221.       (     0, -1.4375, 2.53125),
  222.       (   1.5,       0,     2.4),
  223.       (   1.5,   -0.84,     2.4),
  224.       (  0.84,    -1.5,     2.4),
  225.       (     0,    -1.5,     2.4),
  226.       (  1.75,       0,   1.875),
  227.       (  1.75,   -0.98,   1.875),
  228.       (  0.98,   -1.75,   1.875),
  229.       (     0,   -1.75,   1.875),
  230.       (     2,       0,    1.35),
  231.       (     2,   -1.12,    1.35),
  232.       (  1.12,      -2,    1.35),
  233.       (     0,      -2,    1.35),
  234.       (     2,       0,     0.9),
  235.       (     2,   -1.12,     0.9),
  236.       (  1.12,      -2,     0.9),
  237.       (     0,      -2,     0.9),
  238.       (    -2,       0,     0.9),
  239.       (     2,       0,    0.45),
  240.       (     2,   -1.12,    0.45),
  241.       (  1.12,      -2,    0.45),
  242.       (     0,      -2,    0.45),
  243.       (   1.5,       0,   0.225),
  244.       (   1.5,   -0.84,   0.225),
  245.       (  0.84,    -1.5,   0.225),
  246.       (     0,    -1.5,   0.225),
  247.       (   1.5,       0,    0.15),
  248.       (   1.5,   -0.84,    0.15),
  249.       (  0.84,    -1.5,    0.15),
  250.       (     0,    -1.5,    0.15),
  251.       (  -1.6,       0,   2.025),
  252.       (  -1.6,    -0.3,   2.025),
  253.       (  -1.5,    -0.3,    2.25),
  254.       (  -1.5,       0,    2.25),
  255.       (  -2.3,       0,   2.025),
  256.       (  -2.3,    -0.3,   2.025),
  257.       (  -2.5,    -0.3,    2.25),
  258.       (  -2.5,       0,    2.25),
  259.       (  -2.7,       0,   2.025),
  260.       (  -2.7,    -0.3,   2.025),
  261.       (    -3,    -0.3,    2.25),
  262.       (    -3,       0,    2.25),
  263.       (  -2.7,       0,     1.8),
  264.       (  -2.7,    -0.3,     1.8),
  265.       (    -3,    -0.3,     1.8),
  266.       (    -3,       0,     1.8),
  267.       (  -2.7,       0,   1.575),
  268.       (  -2.7,    -0.3,   1.575),
  269.       (    -3,    -0.3,    1.35),
  270.       (    -3,       0,    1.35),
  271.       (  -2.5,       0,   1.125),
  272.       (  -2.5,    -0.3,   1.125),
  273.       ( -2.65,    -0.3,  0.9375),
  274.       ( -2.65,       0,  0.9375),
  275.       (    -2,    -0.3,     0.9),
  276.       (  -1.9,    -0.3,     0.6),
  277.       (  -1.9,       0,     0.6),
  278.       (   1.7,       0,   1.425),
  279.       (   1.7,   -0.66,   1.425),
  280.       (   1.7,   -0.66,     0.6),
  281.       (   1.7,       0,     0.6),
  282.       (   2.6,       0,   1.425),
  283.       (   2.6,   -0.66,   1.425),
  284.       (   3.1,   -0.66,   0.825),
  285.       (   3.1,       0,   0.825),
  286.       (   2.3,       0,     2.1),
  287.       (   2.3,   -0.25,     2.1),
  288.       (   2.4,   -0.25,   2.025),
  289.       (   2.4,       0,   2.025),
  290.       (   2.7,       0,     2.4),
  291.       (   2.7,   -0.25,     2.4),
  292.       (   3.3,   -0.25,     2.4),
  293.       (   3.3,       0,     2.4),
  294.       (   2.8,       0,   2.475),
  295.       (   2.8,   -0.25,   2.475),
  296.       ( 3.525,   -0.25, 2.49375),
  297.       ( 3.525,       0, 2.49375),
  298.       (   2.9,       0,   2.475),
  299.       (   2.9,   -0.15,   2.475),
  300.       (  3.45,   -0.15,  2.5125),
  301.       (  3.45,       0,  2.5125),
  302.       (   2.8,       0,     2.4),
  303.       (   2.8,   -0.15,     2.4),
  304.       (   3.2,   -0.15,     2.4),
  305.       (   3.2,       0,     2.4),
  306.       (     0,       0,    3.15),
  307.       (   0.8,       0,    3.15),
  308.       (   0.8,   -0.45,    3.15),
  309.       (  0.45,    -0.8,    3.15),
  310.       (     0,    -0.8,    3.15),
  311.       (     0,       0,    2.85),
  312.       (   1.4,       0,     2.4),
  313.       (   1.4,  -0.784,     2.4),
  314.       ( 0.784,    -1.4,     2.4),
  315.       (     0,    -1.4,     2.4),
  316.       (   0.4,       0,    2.55),
  317.       (   0.4,  -0.224,    2.55),
  318.       ( 0.224,    -0.4,    2.55),
  319.       (     0,    -0.4,    2.55),
  320.       (   1.3,       0,    2.55),
  321.       (   1.3,  -0.728,    2.55),
  322.       ( 0.728,    -1.3,    2.55),
  323.       (     0,    -1.3,    2.55),
  324.       (   1.3,       0,     2.4),
  325.       (   1.3,  -0.728,     2.4),
  326.       ( 0.728,    -1.3,     2.4),
  327.       (     0,    -1.3,     2.4),
  328.       (     0,       0,       0),
  329.       ( 1.425,  -0.798,       0),
  330.       (   1.5,       0,   0.075),
  331.       ( 1.425,       0,       0),
  332.       ( 0.798,  -1.425,       0),
  333.       (     0,    -1.5,   0.075),
  334.       (     0,  -1.425,       0),
  335.       (   1.5,   -0.84,   0.075),
  336.       (  0.84,    -1.5,   0.075) );
  337.  
  338.   TeaTex :
  339.     Array[0..1, 0..1, 0..1] of TGLFloat =
  340.       ( ((0, 0), (1, 0)),
  341.         ((0, 1), (1, 1)) );
  342.  
  343.  
  344. procedure DrawBox(Size : TGLFloat; DrawType : TGLenum);
  345. procedure glutWireCube(Size : TglDouble);
  346. procedure glutSolidCube(Size : TglDouble);
  347. procedure glutWireSphere(
  348.   Radius : TglDouble;
  349.   Slices : TGLint;
  350.   Stacks : TGLint);
  351. procedure glutSolidSphere(
  352.   Radius : TglDouble;
  353.   Slices : TGLint;
  354.   Stacks : TGLint);
  355. procedure glutWireCone(
  356.   Base : TglDouble;
  357.   Height : TglDouble;
  358.   Slices : TGLint;
  359.   Stacks : TGLint);
  360. procedure glutSolidCone(
  361.   Base : TglDouble;
  362.   Height : TglDouble;
  363.   Slices : TGLint;
  364.   Stacks : TGLint);
  365. //jhadd
  366. procedure glutWireCylinder(
  367.   Radius : TglDouble;
  368.   Height : TglDouble;
  369.   Slices : TGLint;
  370.   Stacks : TGLint);
  371. procedure glutSolidCylinder(
  372.   Radius : TglDouble;
  373.   Height : TglDouble;
  374.   Slices : TGLint;
  375.   Stacks : TGLint);
  376.  
  377. procedure glutWireTube(
  378.   Radius : TglDouble;
  379.   Height : TglDouble;
  380.   Slices : TGLint;
  381.   Stacks : TGLint);
  382. procedure glutSolidTube(
  383.   Radius : TglDouble;
  384.   Height : TglDouble;
  385.   Slices : TGLint;
  386.   Stacks : TGLint);
  387.  
  388. procedure glutWireTorus(
  389.   innerRadius : TglDouble;
  390.   outerRadius :TglDouble;
  391.   nsides : TGLint;
  392.   rings : TGLint);
  393. procedure glutSolidTorus(
  394.   innerRadius : TglDouble;
  395.   outerRadius :TglDouble;
  396.   nsides : TGLint;
  397.   rings : TGLint);
  398. procedure glutWireDodecahedron;
  399. procedure glutSolidDodecahedron;
  400. procedure Octaheadron(ShadeType : TGLenum);
  401. procedure glutWireOctaheadron;
  402. procedure glutSolidOctaheadron;
  403. procedure Icosahedron(ShadeType : TGLenum);
  404. procedure glutWireIcosahedron;
  405. procedure glutSolidIcosahedron;
  406. procedure Tetrahedron(ShadeType : TGLenum);
  407. procedure glutWireTetrahedron;
  408. procedure glutSolidTetrahedron;
  409.  
  410. { Teapot stuff }
  411. procedure Teapot(Grid : TGLint; Scale : TglDouble; ShadeType : TGLenum);
  412. procedure glutWireTeapot(Scale : TglDouble);
  413. procedure glutSolidTeapot(Scale : TglDouble);
  414.  
  415. { Generally useful stuff }
  416. procedure Diff3(a0, a1, a2, b0, b1, b2 : TGLFloat; var c : array of TGLFloat);
  417. procedure CrossProd(v1, v2 : array of TGLFloat; var prod : array of TGLFloat);
  418. procedure Normalize(var v : array of TGLFloat);
  419.  
  420. var
  421.   quadObj : pGLUquadricObj=nil;
  422.   dodec : array[0..19, 0..2] of TGLFloat;
  423.  
  424. implementation
  425.  
  426. procedure DrawBox(Size : TGLFloat; DrawType : TGLenum);
  427. var
  428.   V : array[0..7, 0..2] of TGLFloat;
  429.   I : TGLint;
  430.   HalfSize : TGLFloat;
  431. begin { DrawBox }
  432.   HalfSize := Size / 2;
  433.   V[0, 0] := -HalfSize;
  434.   V[1, 0] := -HalfSize;
  435.   V[2, 0] := -HalfSize;
  436.   V[3, 0] := -HalfSize;
  437.   V[4, 0] := HalfSize;
  438.   V[5, 0] := HalfSize;
  439.   V[6, 0] := HalfSize;
  440.   V[7, 0] := HalfSize;
  441.  
  442.   V[0, 1] := -HalfSize;
  443.   V[1, 1] := -HalfSize;
  444.   V[4, 1] := -HalfSize;
  445.   V[5, 1] := -HalfSize;
  446.   V[2, 1] := HalfSize;
  447.   V[3, 1] := HalfSize;
  448.   V[6, 1] := HalfSize;
  449.   V[7, 1] := HalfSize;
  450.  
  451.   V[0, 2] := -HalfSize;
  452.   V[3, 2] := -HalfSize;
  453.   V[4, 2] := -HalfSize;
  454.   V[7, 2] := -HalfSize;
  455.   V[1, 2] := HalfSize;
  456.   V[2, 2] := HalfSize;
  457.   V[5, 2] := HalfSize;
  458.   V[6, 2] := HalfSize;
  459.  
  460.   for I := 0 to 5 do
  461.   begin
  462.     glBegin(DrawType);
  463.       glNormal3fv(@BoxPoints[I, 0]);
  464.       glVertex3fv(@V[BoxFaces[I, 0], 0]);
  465.       glVertex3fv(@V[BoxFaces[I, 1], 0]);
  466.       glVertex3fv(@V[BoxFaces[I, 2], 0]);
  467.       glVertex3fv(@V[BoxFaces[I, 3], 0]);
  468.     glEnd;
  469.   end;
  470. end; { DrawBox }
  471.  
  472. procedure glutWireSphere(
  473.   Radius : TglDouble;
  474.   Slices : TGLint;
  475.   Stacks : TGLint);
  476. begin { glutWireSphere }
  477.   if quadObj = nil then
  478.     quadObj := gluNewQuadric;
  479.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  480.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  481.   gluSphere(quadObj, Radius, Slices, Stacks);
  482.   gluDeleteQuadric(quadObj);
  483.   QuadObj:=nil;
  484. end; { glutWireSphere }
  485.  
  486. procedure glutSolidSphere(
  487.   Radius : TglDouble;
  488.   Slices : TGLint;
  489.   Stacks : TGLint);
  490. begin { glutSolidSphere }
  491.   if quadObj = nil then
  492.     quadObj := gluNewQuadric;
  493.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  494.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  495.   gluSphere(quadObj, Radius, Slices, Stacks);
  496.   gluDeleteQuadric(quadObj);
  497.   QuadObj:=nil;
  498. end; { glutSolidSphere }
  499.  
  500. procedure glutWireCube(Size : TglDouble);
  501. begin { glutWireCube }
  502.   DrawBox(Size, GL_LINE_LOOP);
  503. end; { glutWireCube }
  504.  
  505. procedure glutSolidCube(Size : TglDouble);
  506. begin { glutSolidCube }
  507.   DrawBox(Size, GL_QUADS);
  508. end; { glutSolidCube }
  509.  
  510. procedure glutWireCone(
  511.   Base : TglDouble;
  512.   Height : TglDouble;
  513.   Slices : TGLint;
  514.   Stacks : TGLint);
  515. begin { glutWireCone }
  516.   if quadObj = nil then
  517.     quadObj := gluNewQuadric;
  518.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  519.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  520.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  521.   gluDeleteQuadric(quadObj);
  522.   QuadObj:=nil;
  523. end; { glutWireCone }
  524.  
  525. procedure glutSolidCone(
  526.   Base : TglDouble;
  527.   Height : TglDouble;
  528.   Slices : TGLint;
  529.   Stacks : TGLint);
  530. begin { glutSolidCone }
  531.   if quadObj = nil then
  532.     quadObj := gluNewQuadric;
  533.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  534.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  535.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  536.   gluDeleteQuadric(quadObj);
  537.   QuadObj:=nil;
  538. end; { glutSolidCone }
  539.  //jhadd
  540. procedure glutWireCylinder(
  541.   Radius : TglDouble;
  542.   Height : TglDouble;
  543.   Slices : TGLint;
  544.   Stacks : TGLint);
  545. begin { glutWireCylinder }
  546.   if quadObj = nil then
  547.     quadObj := gluNewQuadric;
  548.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  549.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  550.   glPushMatrix;
  551.   glTranslatef(0.0,0.0,height/2);
  552.   gluDisk(    quadObj, 0.0, Radius, slices,stacks);
  553.   glPopMatrix;
  554.   glPushMatrix;
  555.   glTranslatef(0.0,0.0,-height/2);
  556.   gluDisk(    quadObj, 0.0, Radius,slices,stacks);
  557.   gluCylinder(quadObj, Radius, Radius, height, slices, stacks);
  558.   glPopMatrix;
  559.   gluDeleteQuadric(quadObj);
  560.   QuadObj:=nil;
  561. end; { glutWireCylinder }
  562.  
  563. procedure glutSolidCylinder(
  564.   Radius : TglDouble;
  565.   Height : TglDouble;
  566.   Slices : TGLint;
  567.   Stacks : TGLint);
  568. begin { glutSolidCylinder }
  569.   if quadObj = nil then
  570.     quadObj := gluNewQuadric;
  571.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  572.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  573.   glPushMatrix;
  574.   glTranslatef(0.0,0.0,height/2);
  575.   gluDisk(    quadObj, 0.0, Radius, slices,stacks);
  576.   glPopMatrix;
  577.   glPushMatrix;
  578.   glTranslatef(0.0,0.0,-height/2);
  579.   gluDisk(    quadObj, 0.0, Radius,slices,stacks);
  580.   gluCylinder(quadObj, Radius, Radius, height, slices, stacks);
  581.   glPopMatrix;
  582.   gluDeleteQuadric(quadObj);
  583.   QuadObj:=nil;
  584. end; { glutSolidCylinder }
  585.  
  586. procedure glutWireTube(
  587.   Radius : TglDouble;
  588.   Height : TglDouble;
  589.   Slices : TGLint;
  590.   Stacks : TGLint);
  591. begin { glutWireTube }
  592.   if quadObj = nil then
  593.     quadObj := gluNewQuadric;
  594.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  595.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  596.   glPushMatrix;
  597.   glTranslatef(0.0,0.0,-height/2);
  598.   gluCylinder(quadObj, Radius, Radius, height, slices, stacks);
  599.   glPopMatrix;
  600.   gluDeleteQuadric(quadObj);
  601.   QuadObj:=nil;
  602. end; { glutWireTube }
  603.  
  604. procedure glutSolidTube(
  605.   Radius : TglDouble;
  606.   Height : TglDouble;
  607.   Slices : TGLint;
  608.   Stacks : TGLint);
  609. begin { glutSolidTube }
  610.   if quadObj = nil then
  611.     quadObj := gluNewQuadric;
  612.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  613.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  614.   glPushMatrix;
  615.   glTranslatef(0.0,0.0,-height/2);
  616.   gluCylinder(quadObj, Radius, Radius, height, slices, stacks);
  617.   glPopMatrix;
  618.   gluDeleteQuadric(quadObj);
  619.   QuadObj:=nil;
  620. end; { glutSolidTube }
  621.  
  622. procedure Doughnut(
  623.   inr : TGLFloat;
  624.   outR : TGLFloat;
  625.   nsides : TGLint;
  626.   rings : TGLint;
  627.   DrawType :TGLenum);
  628. var
  629.   i, j : integer;
  630.   theta, phi, theta1, phi1 : TGLFloat;
  631.   p0, p1, p2, p3,
  632.   n0, n1, n2, n3 : array[0..2] of TGLFloat;
  633. begin { Doughnut }
  634.   for i := 0 to rings - 1 do
  635.   begin
  636.     theta := i *2.0 * PI / rings;
  637.     theta1 := (i + 1) * 2.0 * PI / rings;
  638.     for j := 0 to nsides - 1 do
  639.     begin
  640.       phi := j *2.0 * PI / nsides;
  641.       phi1 := (j + 1) * 2.0 * PI / nsides;
  642.  
  643.         p0[0] := cos(theta) * (outR + inr * cos(phi));
  644.       p0[1] := -sin(theta) * (outR + inr * cos(phi));
  645.       p0[2] := inr * sin(phi);
  646.  
  647.         p1[0] := cos(theta1) * (outR + inr * cos(phi));
  648.       p1[1] := -sin(theta1) * (outR + inr * cos(phi));
  649.       p1[2] := inr * sin(phi);
  650.  
  651.         p2[0] := cos(theta1) * (outR + inr * cos(phi1));
  652.       p2[1] := -sin(theta1) * (outR + inr * cos(phi1));
  653.       p2[2] := inr * sin(phi1);
  654.  
  655.       p3[0] := cos(theta) * (outR + inr * cos(phi1));
  656.       p3[1] := -sin(theta) * (outR + inr * cos(phi1));
  657.       p3[2] := inr * sin(phi1);
  658.  
  659.       n0[0] := cos(theta) * (cos(phi));
  660.       n0[1] := -sin(theta) * (cos(phi));
  661.       n0[2] := sin(phi);
  662.  
  663.       n1[0] := cos(theta1) * (cos(phi));
  664.       n1[1] := -sin(theta1) * (cos(phi));
  665.       n1[2] := sin(phi);
  666.  
  667.       n2[0] := cos(theta1) * (cos(phi1));
  668.       n2[1] := -sin(theta1) * (cos(phi1));
  669.       n2[2] := sin(phi1);
  670.  
  671.       n3[0] := cos(theta) * (cos(phi1));
  672.       n3[1] := -sin(theta) * (cos(phi1));
  673.       n3[2] := sin(phi1);
  674.  
  675.       glBegin(DrawType);
  676.       glNormal3fv(@n3[0]);
  677.       glVertex3fv(@p3[0]);
  678.       glNormal3fv(@n2[0]);
  679.       glVertex3fv(@p2[0]);
  680.       glNormal3fv(@n1[0]);
  681.       glVertex3fv(@p1[0]);
  682.       glNormal3fv(@n0[0]);
  683.       glVertex3fv(@p0[0]);
  684.       glEnd();
  685.     end;
  686.   end;
  687. end; { Doughnut }
  688.  
  689. procedure glutWireTorus(
  690.   innerRadius : TglDouble;
  691.   outerRadius :TglDouble;
  692.   nsides : TGLint;
  693.   rings : TGLint);
  694. begin { glutWireTorus }
  695.   Doughnut(innerRadius, outerRadius, nsides, rings, GL_LINE_LOOP);
  696. end; { glutWireTorus }
  697.  
  698. procedure glutSolidTorus(
  699.   innerRadius : TglDouble;
  700.   outerRadius :TglDouble;
  701.   nsides : TGLint;
  702.   rings : TGLint);
  703. begin { glutSolidTorus }
  704.   Doughnut(innerRadius, outerRadius, nsides, rings, GL_QUADS);
  705. end; { glutSolidTorus }
  706.  
  707. procedure initDodecahedron;
  708. var
  709.   alpha, beta : TGLFloat;
  710. begin { initDodecahedron }
  711.   alpha := sqrt(2.0 / (3.0 + sqrt(5.0)));
  712.   beta := 1.0 + sqrt(6.0 / (3.0 + sqrt(5.0)) -
  713.     2.0 + 2.0 * sqrt(2.0 / (3.0 + sqrt(5.0))));
  714.  
  715.   dodec[0, 0] := -alpha;  dodec[0, 1] := 0;       dodec[0, 2] := beta;
  716.   dodec[1, 0] := alpha;   dodec[1, 1] := 0;       dodec[1, 2] := beta;
  717.   dodec[2, 0] := -1;      dodec[2, 1] := -1;      dodec[2, 2] := -1;
  718.   dodec[3, 0] := -1;      dodec[3, 1] := -1;      dodec[3, 2] := 1;
  719.   dodec[4, 0] := -1;      dodec[4, 1] := 1;       dodec[4, 2] := -1;
  720.   dodec[5, 0] := -1;      dodec[5, 1] := 1;       dodec[5, 2] := 1;
  721.   dodec[6, 0] := 1;       dodec[6, 1] := -1;      dodec[6, 2] := -1;
  722.   dodec[7, 0] := 1;       dodec[7, 1] := -1;      dodec[7, 2] := 1;
  723.   dodec[8, 0] := 1;       dodec[8, 1] := 1;       dodec[8, 2] := -1;
  724.   dodec[9, 0] := 1;       dodec[9, 1] := 1;       dodec[9, 2] := 1;
  725.   dodec[10, 0] := beta;   dodec[10, 1] := alpha;  dodec[10, 2] := 0;
  726.   dodec[11, 0] := beta;   dodec[11, 1] := -alpha; dodec[11, 2] := 0;
  727.   dodec[12, 0] := -beta;  dodec[12, 1] := alpha;  dodec[12, 2] := 0;
  728.   dodec[13, 0] := -beta;  dodec[13, 1] := -alpha; dodec[13, 2] := 0;
  729.   dodec[14, 0] := -alpha; dodec[14, 1] := 0;      dodec[14, 2] := -beta;
  730.   dodec[15, 0] := alpha;  dodec[15, 1] := 0;      dodec[15, 2] := -beta;
  731.   dodec[16, 0] := 0;      dodec[16, 1] := beta;   dodec[16, 2] := alpha;
  732.   dodec[17, 0] := 0;      dodec[17, 1] := beta;   dodec[17, 2] := -alpha;
  733.   dodec[18, 0] := 0;      dodec[18, 1] := -beta;  dodec[18, 2] := alpha;
  734.   dodec[19, 0] := 0;      dodec[19, 1] := -beta;  dodec[19, 2] := -alpha;
  735. end; { initDodecaheadron }
  736.  
  737. procedure Diff3(
  738.   a0, a1, a2, b0, b1, b2 : TGLFloat;
  739.   var c : array of TGLFloat);
  740. begin { Diff3 }
  741.   c[0] := a0 - b0;
  742.   c[1] := a1 - b1;
  743.   c[2] := a2 - b2;
  744. end; { Diff3 }
  745.  
  746. procedure CrossProd(
  747.   v1, v2 : array of TGLFloat;
  748.   var prod : array of TGLFloat);
  749. var
  750.   p : array[0..2] of TGLFloat;
  751. begin { CrossProd }
  752.   p[0] := v1[1] * v2[2] - v2[1] * v1[2];
  753.   p[1] := v1[2] * v2[0] - v2[2] * v1[0];
  754.   p[2] := v1[0] * v2[1] - v2[0] * v1[1];
  755.   prod[0] := p[0];
  756.   prod[1] := p[1];
  757.   prod[2] := p[2];
  758. end; { CrossProd }
  759.  
  760. procedure Normalize(var v : array of TGLFloat);
  761. var
  762.   d : TGLFloat;
  763. begin { Normalize }
  764.   d := sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  765.   d := 1 / d;
  766.   v[0] := v[0] * d;
  767.   v[1] := v[1] * d;
  768.   v[2] := v[2] * d;
  769. end; { Normalize }
  770.  
  771. procedure Pentagon(a, b, c, d, e : integer; shadeType : TGLenum);
  772. var
  773.   n0, d1, d2 : array[0..2] of TGLFloat;
  774. begin { Pentagon }
  775.   Diff3(dodec[a, 0], dodec[a, 1], dodec[1, 2],
  776.         dodec[b, 0], dodec[b, 1], dodec[b, 2], d1);
  777.   Diff3(dodec[b, 0], dodec[b, 1], dodec[b, 2],
  778.         dodec[c, 0], dodec[c, 1], dodec[c, 2], d2);
  779.   CrossProd(d1, d2, n0);
  780.   Normalize(n0);
  781.  
  782.   glBegin(shadeType);
  783.     glNormal3fv(@n0[0]);
  784.     glVertex3fv(@dodec[a, 0]);
  785.     glVertex3fv(@dodec[b, 0]);
  786.     glVertex3fv(@dodec[c, 0]);
  787.     glVertex3fv(@dodec[d, 0]);
  788.     glVertex3fv(@dodec[e, 0]);
  789.   glEnd();
  790. end; { Pentagon }
  791.  
  792. procedure Dodecahedron(DrawType : TGLenum);
  793. begin { Dodecahedron }
  794.   pentagon(0, 1, 9, 16, 5, DrawType);
  795.   pentagon(1, 0, 3, 18, 7, DrawType);
  796.   pentagon(1, 7, 11, 10, 9, DrawType);
  797.   pentagon(11, 7, 18, 19, 6, DrawType);
  798.   pentagon(8, 17, 16, 9, 10, DrawType);
  799.   pentagon(2, 14, 15, 6, 19, DrawType);
  800.   pentagon(2, 13, 12, 4, 14, DrawType);
  801.   pentagon(2, 19, 18, 3, 13, DrawType);
  802.   pentagon(3, 0, 5, 12, 13, DrawType);
  803.   pentagon(6, 15, 8, 10, 11, DrawType);
  804.   pentagon(4, 17, 8, 15, 14, DrawType);
  805.   pentagon(4, 12, 5, 16, 17, DrawType);
  806. end; { Dodecahedron }
  807.  
  808. procedure glutWireDodecahedron;
  809. begin { glutWireDodecahedron }
  810.   Dodecahedron(GL_LINE_LOOP);
  811. end; { glutWireDodecahedron }
  812.  
  813. procedure glutSolidDodecahedron;
  814. begin { glutSolidDodecahedron }
  815.   Dodecahedron(GL_TRIANGLE_FAN);
  816. end; { glutSolidDodecahedron }
  817.  
  818. procedure RecordItem(n1, n2, n3 :  array of TGLFloat; ShadeType : TGLenum);
  819. var
  820.   q0, q1 : array[0..2] of TGLFloat;
  821. begin { RecordItem }
  822.   Diff3(n1[0], n1[1], n1[2], n2[0], n2[1], n2[2], q0);
  823.   Diff3(n2[0], n2[1], n2[2], n3[0], n3[1], n3[2], q1);
  824.   CrossProd(q0, q1, q1);
  825.   Normalize(q1);
  826.  
  827.   glBegin(shadeType);
  828.     glNormal3fv(@q1[0]);
  829.     glVertex3fv(@n1[0]);
  830.     glVertex3fv(@n2[0]);
  831.     glVertex3fv(@n3[0]);
  832.   glEnd;
  833. end; { RecordItem }
  834.  
  835. procedure SubDivide(
  836.   v0, v1, v2 : array of TGLFloat;
  837.   ShadeType : TGLenum);
  838. var
  839.   Depth : Integer;
  840.   w0, w1, w2 : array[0..2] of TGLFloat;
  841.   l : TGLFloat;
  842.   i, j, k, n : Integer;
  843. begin { SubDivide }
  844.   Depth := 1;
  845.   for i := 0 to Depth - 1 do
  846.   begin
  847.     j := 0;
  848.     while i + j < Depth do
  849.     begin
  850.       k := depth - i - j;
  851.       for n := 0 to 2 do
  852.       begin
  853.         w0[n] := (i * v0[n] + j * v1[n] + k * v2[n]) / depth;
  854.         w1[n] := ((i + 1) * v0[n] + j * v1[n] + (k - 1) * v2[n])
  855.           / depth;
  856.         w2[n] := (i * v0[n] + (j + 1) * v1[n] + (k - 1) * v2[n])
  857.           / depth;
  858.       end;
  859.       l := sqrt(w0[0] * w0[0] + w0[1] * w0[1] + w0[2] * w0[2]);
  860.       w0[0] := w0[0] / l;
  861.       w0[1] := w0[1] / l;
  862.       w0[2] := w0[2] / l;
  863.       l := sqrt(w1[0] * w1[0] + w1[1] * w1[1] + w1[2] * w1[2]);
  864.       w1[0] := w1[0] / l;
  865.       w1[1] := w1[1] / l;
  866.       w1[2] := w1[2] / l;
  867.       l := sqrt(w2[0] * w2[0] + w2[1] * w2[1] + w2[2] * w2[2]);
  868.       w2[0] := w2[0] / l;
  869.       w2[1] := w2[1] / l;
  870.       w2[2] := w2[2] / l;
  871.       RecordItem(w1, w0, w2, ShadeType);
  872.       Inc(j);
  873.     end;
  874.   end;
  875. end; { SubDivide }
  876.  
  877. procedure DrawTriangle(
  878.   I : Integer;
  879.   Data : Array of TGLfloat3v;
  880.   NDX :  Array of TInteger3v;
  881.   ShadeType : TGLenum);
  882. var
  883.   X0, X1, X2 : TGLfloat3v;
  884. begin { DrawTriangle }
  885.   X0 := Data[NDX[i, 0]];
  886.   X1 := Data[NDX[i, 1]];
  887.   X2 := Data[NDX[i, 2]];
  888.   SubDivide(X0, X1, X2, ShadeType);
  889. end; { DrawTriangle }
  890.  
  891. procedure Octaheadron(ShadeType : TGLenum);
  892. var
  893.   I : Integer;
  894. begin { Octaheadron }
  895.   for I := 0 to 7 do
  896.     DrawTriangle(I, OctData, OctIndex, ShadeType);
  897. end; { Octaheadron }
  898.  
  899. procedure glutWireOctaheadron;
  900. begin { glutWireOctaheadron }
  901.   Octaheadron(GL_LINE_LOOP);
  902. end; { glutWireOctaheadron }
  903.  
  904. procedure glutSolidOctaheadron;
  905. begin { glutSolidOctaheadron }
  906.   Octaheadron(GL_TRIANGLES);
  907. end; { glutSolidOctaheadron }
  908.  
  909. procedure Icosahedron(ShadeType : TGLenum);
  910. var
  911.   I : Integer;
  912. begin { Icosahedron }
  913.   for I := 0 to 19 do
  914.     DrawTriangle(I, IcoData, IcoIndex, ShadeType);
  915. end; { Icosahedron }
  916.  
  917. procedure glutWireIcosahedron;
  918. begin { glutWireIcosahedron }
  919.   Icosahedron(GL_LINE_LOOP);
  920. end; { glutWireIcosahedron }
  921.  
  922. procedure glutSolidIcosahedron;
  923. begin { glutSolidIcosahedron }
  924.   Icosahedron(GL_TRIANGLES);
  925. end; { glutSolidIcosahedron }
  926.  
  927. procedure Tetrahedron(ShadeType : TGLenum);
  928. var
  929.   I : Integer;
  930. begin { Tetrahedron }
  931.   for I := 0 to 3 do
  932.     DrawTriangle(I, TetData, TetIndex, ShadeType);
  933. end; { Tetrahedron }
  934.  
  935. procedure glutWireTetrahedron;
  936. begin { glutWireTetrahedron }
  937.   Tetrahedron(GL_LINE_LOOP);
  938. end; { glutWireTetrahedron }
  939.  
  940. procedure glutSolidTetrahedron;
  941. begin { glutSolidTetrahedron }
  942.   Tetrahedron(GL_TRIANGLES);
  943. end; { glutSolidTetrahedron }
  944.  
  945. { Teapot stuff }
  946.  
  947. procedure Teapot(Grid : TGLint; Scale : TglDouble; ShadeType : TGLenum);
  948. var
  949.   P, Q, R, S : Array[0..3, 0..3, 0..2] of TGLFloat;
  950.   I, J, K, L : TGLint;
  951. begin { Teapot }
  952.   glPushAttrib(GL_ENABLE_BIT or GL_EVAL_BIT);
  953.   glEnable(GL_AUTO_NORMAL);
  954.   glEnable(GL_NORMALIZE);
  955.   glEnable(GL_MAP2_VERTEX_3);
  956.   glEnable(GL_MAP2_TEXTURE_COORD_2);
  957.   glPushMatrix;
  958.   glRotatef(270.0, 1.0, 0.0, 0.0);
  959.   glScalef(0.5 * Scale, 0.5 * Scale, 0.5 * Scale);
  960.   glTranslatef(0.0, 0.0, -1.5);
  961.   for I := 0 to 9 do
  962.   begin
  963.     for J := 0 to 3 do
  964.       for K := 0 to 3 do
  965.         for L := 0 to 2 do
  966.         begin
  967.           P[J, K, L] := TeaData[PatchData[I, J * 4 + K], L];
  968.           Q[J, K, L] := TeaData[PatchData[I, J * 4 + (3 - K)], L];
  969.           if L = 1 then
  970.             Q[J, K, L] := -Q[J, K, L];
  971.           if i < 6 then
  972.           begin
  973.             R[J, K, L] := TeaData[PatchData[I, J * 4 + (3 - K)], L];
  974.             if L = 0 then
  975.               R[J, K, L] := -R[J, K, L];
  976.             S[J, K, L] := TeaData[PatchData[I, J * 4 + K], L];
  977.             if L = 0 then
  978.               S[J, K, L] := -S[J, K, L];
  979.             if L = 1 then
  980.               S[J, K, L] := -S[J, K, L];
  981.           end;
  982.         end;
  983.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, @TeaTex[0,0,0]);
  984.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @P[0, 0, 0]);
  985.     glMapGrid2f(Grid, 0.0, 1.0, Grid, 0.0, 1.0);
  986.     glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  987.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @Q[0, 0, 0]);
  988.     glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  989.     if I < 6 then
  990.     begin
  991.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @R[0, 0, 0]);
  992.       glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  993.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @S[0, 0, 0]);
  994.       glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  995.     end;
  996.   end;
  997.   glPopMatrix;
  998.   glPopAttrib;
  999. end; { Teapot }
  1000.  
  1001. procedure glutWireTeapot(Scale : TglDouble);
  1002. begin { glutWireTeapot }
  1003.   Teapot(10, Scale, GL_LINE);
  1004. end; { glutWireTeapot }
  1005.  
  1006. procedure glutSolidTeapot(Scale : TglDouble);
  1007. begin { glutSolidTeapot }
  1008.   Teapot(14, Scale, GL_FILL);
  1009. end; { glutSolidTeapot }
  1010.  
  1011. initialization
  1012.   initDodecahedron;
  1013.  
  1014. end.
  1015.